//
//  TotalVC.m
//  MyTest
//
//  Created by zhiyou on 16-4-15.
//  Copyright (c) 2016年 wangyang. All rights reserved.
//

#import "TotalVC.h"
#import "WJDropdownMenu.h"
#import "Total.h"
#import "AllCell.h"

#define BASE_URL @"http://www.shihuo.cn/app2/getDaigouList?platform=ios&timestamp=1460377629&v=4.0.2&token=2c915907b710be764786fd2c014af5b6&brand=&c=0&keywords=&page=1&price=&r=0&sort="

@interface TotalVC () <WJMenuDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bgView;
@end

@implementation TotalVC
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    _dataArr = [[NSMutableArray alloc] init];
    [self setTitleBar];
    
    [self getData];
    [self.view addSubview:self.tableView];
    [self setDropMenu];
    [self setMenu];
}

#pragma mark -- 初始化tableView
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 104, Width, Height-104) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[AllCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AllCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.total = _dataArr[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}
#pragma mark -- 获取数据
- (void)getData
{
    [MyRequest getWithURL:BASE_URL parameters:nil success:^(NSDictionary *dic) {
        NSArray *arr = [[dic objectForKey:@"data"] objectForKey:@"info"];
        for (NSDictionary *dic1 in arr) {
            Total *total = [[Total alloc] initWithDictionary:dic1];
            [_dataArr addObject:total];
        }
        [_tableView reloadData];
        
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark -- WJDropMenu

- (void)setDropMenu
{
    NSArray *fourMenuArray = @[@"人气最高",@"全部价格",@"全部品牌",@"全部类型"];
    NSArray *firstArrOne = [NSArray arrayWithObjects:@"人气最高",@"最新发布",@"价格从高到低",@"价格从低到高", nil];
    NSArray *firstMenu = [NSArray arrayWithObjects:firstArrOne, nil];
    NSArray *secondArr = [NSArray arrayWithObjects:@"全部价格",@"100一下",@"100-299",@"300-499",@"500-999",@"1000以上", nil];
    NSArray *secondMenu = [NSArray arrayWithObjects:secondArr, nil];
    
    NSArray *threeArr = [NSArray arrayWithObjects:@"全部品牌",@"其他",@"under Armour",@"adidas",@"New Balance",@"Asics",@"Columbia",@"Tommy Hilfiger", nil];
    NSArray *threeMenu = [NSArray arrayWithObjects:threeArr, nil];
    NSArray *fourArrOne = [NSArray arrayWithObjects:@"全部",@"运动户外",@"休闲鞋服",@"饰品手表",@"食品营养",@"居家百货",@"箱包手袋",@"个人护理", nil];
    NSArray *fourArrTwo = @[@[@"全部类型"],@[@"全部运动户外",@"运动用品",@"运动鞋",@"运动服饰",@"户外装备",@"健身器材",@"健身器材",@"运动装备",@"户外鞋服"],@[@"全部休闲鞋服",@"男装",@"男鞋",@"内衣袜子",@"帽子",@"童装童鞋",@"女装",@"女鞋"],@[@"",@"全部饰品手表",@"男表",@"女表",@"皮带",@"眼镜",@"领带",@"围巾"],@[@"全部食品营养",@"休闲零食",@"基础营养",@"男性营养",@"女性营养",@"健康仪器",@"减脂增肌"],@[@"全部居家百货",@"厨房用具",@"生活用品",@"成人用品",@"剃须刀",@"保温杯",@"儿童玩具",@"家居安全"],@[@"全部箱包手袋",@"精品男包",@"潮流女包",@"拉杆箱",@"户外/运动包",@"公文包",@"钱包",@"电脑数码包"],@[@"全部个人护理",@"美发护发",@"面部护肤",@"身体护肤",@"女性护理",@"口腔护理",@"眼睛护理",@"美容仪/洁面器"]];
    NSArray *fourMenu = [NSArray arrayWithObjects:fourArrOne,fourArrTwo, nil];
    WJDropdownMenu * menu = [[WJDropdownMenu alloc] initWithFrame:CGRectMake(0, 64, Width, 40)];
    menu.delegate = self;
    [self.view addSubview:menu];
    menu.CarverViewColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.3];//设置遮罩层颜色
    
    [menu createFourMenuTitleArray:fourMenuArray FirstArr:firstMenu SecondArr:secondMenu threeArr:threeMenu fourArr:fourMenu];
}

#pragma WJDropdownMenu 的代理
- (void)menuCellDidSelected:(NSInteger)MenuTitleIndex firstIndex:(NSInteger)firstIndex andSecondIndex:(NSInteger)secondIndex
{
    NSLog(@"菜单数:%d 一级菜单数:%d ",MenuTitleIndex,firstIndex);
    if (MenuTitleIndex == 0)
    {
    }else
    {
        
    }
}
- (void)menuCellDidSelected:(NSString *)MenuTitle firstContent:(NSString *)firstContent andSecondContent:(NSString *)secondContent
{
    NSLog(@"菜单title:%@  一级菜单:%@",MenuTitle,firstContent);
    
}
#pragma mark -- 创建导航
- (void)setTitleBar
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, 64)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:bgView.frame];
    imageView.image = [UIImage imageNamed:@"topbar"];
    [bgView addSubview:imageView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 20, 50, 40);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"F_iocn_normal"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:backBtn];
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectZero;
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"D_iocn_normal"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:menuBtn];
    
    [menuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView.mas_top).offset(20);
        make.right.offset(-10);
        make.width.offset(50);
        make.height.offset(40);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.textAlignment = NSTextAlignmentLeft;
    UIImage * buttonImage = [UIImage  imageNamed:@"icon_search"];
    
    UIImage * stretchableButtonImage = [buttonImage  stretchableImageWithLeftCapWidth:8  topCapHeight:8];
    
    [button setImage:stretchableButtonImage forState:UIControlStateNormal];
    [button setTitle:@"搜索商品" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.clipsToBounds = YES;
    button.layer.cornerRadius = 5;
    button.titleLabel.font = [UIFont systemFontOfSize:12];
    
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft ;
    
    [button setBackgroundColor:[UIColor whiteColor]];
    [bgView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backBtn.mas_right).offset(5);
        make.right.equalTo(menuBtn.mas_left).offset(-5);
        make.bottom.equalTo(bgView.mas_bottom).offset(-10);
        make.height.offset(25);
    }];
}
- (void)btnClick
{
    
}
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)showMenu
{
    static int count = 1;
    if (count%2 == 1) {
        
        [self showView];
    }else
    {
        [self hideView];
    }
    count ++;
}
- (void)setMenu
{
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(Width*2/3, 64, Width/3, 0)];
    _bgView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_bgView];
    
    NSArray *arr = @[@"首页",@"我的",@"消息"];
    for (int i = 0; i < 3; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 30*i, Width/3, 30);
        button.tag = i;
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:button];
        _bgView.clipsToBounds = YES;
    }
}
- (void)showView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    _bgView.frame = CGRectMake(Width*2/3, 64, Width/3, 90);
    [UIView commitAnimations];
}
- (void)hideView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    _bgView.frame = CGRectMake(Width*2/3, 64, Width/3, 0);
    [UIView commitAnimations];
}
- (void)clickButton:(UIButton *)button
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
